package com.epcs.listener;

import javax.servlet.http.HttpSession;

import com.epcs.model.bo.NotificationBO;
import com.epcs.model.dto.Account;
import com.epcs.util.EpcsDateTimeUtil;

public class CommingEvent implements Runnable{
	private HttpSession currentSession;
	private long minutes;
	private boolean stop = false;



	@Override
	public void run() {
		try {
			while (!stop) {
			
				Thread.sleep(minutes);
				if (currentSession != null) {
					NotificationBO notifyBO = new NotificationBO();
					Account onlineUser = (Account) currentSession.getAttribute("USER_INFO");
					if (onlineUser != null) {
						notifyBO.createNotificationForCommingEvent(onlineUser);
						System.out.println("-----Kết thúc tạo notification lúc "
								+EpcsDateTimeUtil.getCurrentIsoByPattern("HH:mm dd/MM/yyyy")+"----");
					}
				} else {
					// hết session thì tự ngắt
					this.stop = true;
				}
				
			}
		} catch (Exception e) {
			System.out.println("ERROR thread: " + e.getMessage());
		}
		
		System.out.println("Stop auto notify");
	}
		
	
	public CommingEvent(HttpSession currentSession) {
		super();
		this.currentSession = currentSession;
		this.minutes = 60000;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}


	public HttpSession getCurrentSession() {
		return currentSession;
	}


	public void setCurrentSession(HttpSession currentSession) {
		this.currentSession = currentSession;
	}
}
